package hk.ust.comp3111.ustnutrition.data.db;

import java.util.Map;

import hk.ust.comp3111.ustnutrition.MiscUtils;
import hk.ust.comp3111.ustnutrition.data.DataRow;
import hk.ust.comp3111.ustnutrition.data.DataTableName;

@DataTableName(value="foods",canWrite=false)
public class Food extends DataRow
{
	public Food()
	{
	}
	
	public Food(String name, String photo, Restaurant restaurant)
	{
		mName = name;
		mPhoto = photo;
		mRestaurant = restaurant;
		
	}

	public Food(String name, String photo, Restaurant restaurant, double energy, double carbohydrate, double protein, double fat, double sodium)
	{
		mName = name;
		mPhoto = photo;
		mRestaurant = restaurant;
		mEnergy = energy;
		mCarbohydrate = carbohydrate;
		mProtein = protein;
		mFat = fat;
		mSodium = sodium;
	}

	public String getName()
	{
		return mName;
	}

	public String getPhoto()
	{
		return mPhoto;
	}
	
	public Restaurant getRestaurant()
	{
		return mRestaurant;
	}

	public String getRestaurantStr()
	{
		return mRestaurant == null ? "<No Restuarant Info>" : mRestaurant.toString();
	}

	public double getEnergy()
	{
		return mEnergy;
	}
	
	public double getCarbohydrate()
	{
		return mCarbohydrate;
	}

	public double getProtein()
	{
		return mProtein;
	}

	public double getFat()
	{
		return mFat;
	}

	public double getSodium()
	{
		return mSodium;
	}

	/**
	 * Return the user rating of this food
	 *
	 * @return The rating, [0, 10]
	 */
	public double getUserRating()
	{
		if(mScoreNum == 0)
			return 0;
		else
			return (double)mScore / mScoreNum;
	}

	private String mName;
	private String mPhoto;
	private Restaurant mRestaurant;
	private double mEnergy;
	private double mCarbohydrate;
	private double mProtein;
	private double mFat;
	private double mSodium;
	private int mScore;
	private int mScoreNum;

	@Override
	public void loadInfo(Map<String, Object> obj) {
		super.loadInfo(obj);
		mName = (String) obj.get("name");
		mPhoto = (String) obj.get("photo");
		mRestaurant = get((String)obj.get("restaurant"), Restaurant.class);
		mEnergy = MiscUtils.get(obj, "energy", 0.0);
		mCarbohydrate = MiscUtils.get(obj, "carbohydrate", 0.0);
		mProtein = MiscUtils.get(obj, "protein", 0.0);
		mFat = MiscUtils.get(obj, "fat", 0.0);
		mSodium = MiscUtils.get(obj, "sodium", 0.0);
		mScore = MiscUtils.get(obj, "score", 0);
		mScoreNum = MiscUtils.get(obj, "scoreNum", 0);
	}

	@Override
	public void putInfo(Map<String, Object> obj) {
		super.putInfo(obj);
		obj.put("name", mName);
		obj.put("photo", mPhoto);
		obj.put("restaurant", getId(mRestaurant));
		obj.put("energy", mEnergy);
		obj.put("carbohydrate", mCarbohydrate);
		obj.put("protein", mProtein);
		obj.put("fat", mFat);
		obj.put("sodium", mSodium);
		obj.put("score", mScore);
		obj.put("scoreNum", mScoreNum);
	}
}
